package com.moi.summer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;



public class SecondActivity extends FragmentActivity implements View.OnClickListener {


    //跳转传递的信息
    public static void actionStart(Context context, String data) {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra("text", data);
        context.startActivity(intent);
    }


    private TextView tab1;
    private TextView tab2;
    private TextView tab3;
    private View color;//滑条
    private ViewPager mViewPager;
    private LinearLayout tabLayout;
    protected static View mView1;
    protected static View mView2;
    protected static View mView3;
    protected List<View> viewList;//用来存放view的list
    private List<Fragment> fraglist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_second);

        initView();
    }

    private void initView() {

        tab1 = (TextView) findViewById(R.id.tv_1);
        tab1.setOnClickListener(this);
        tab2 = (TextView) findViewById(R.id.tv_2);
        tab2.setOnClickListener(this);
        tab3 = (TextView) findViewById(R.id.tv_3);
        tab3.setOnClickListener(this);

        tab1.setTextColor(getResources().getColor(R.color.lightsalmon));

        tab2.setTextColor(getResources().getColor(R.color.skyblue));
        tab3.setTextColor(getResources().getColor(R.color.skyblue));//先全部都设为黑色，然后再给对应的textview设置颜色

        color = findViewById(R.id.color);

        int width = getScreenWidth(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width / 3, 10);
        color.setLayoutParams(params);//设置滑条的宽为屏幕的三分之一

        mViewPager = (ViewPager) findViewById(R.id.viewPager);

        tabLayout = (LinearLayout) findViewById(R.id.tabLayout);

        LayoutInflater lf = getLayoutInflater().from(this);//注意这个玩意的用法
        mView1 = lf.inflate(R.layout.hello, tabLayout, false);
        mView2 = lf.inflate(R.layout.list_view_layout, tabLayout, false);
        mView3 = lf.inflate(R.layout.download, tabLayout, false);//将xml扩充为view对象

        viewList = new ArrayList<View>();
        viewList.add(mView1);
        viewList.add(mView2);
        viewList.add(mView3);

        fraglist = new ArrayList<Fragment>();
        fraglist.add(new HelloFragment());
        fraglist.add(new CharacterListView());
        fraglist.add(new DownloadFragment());

        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fraglist);
        mViewPager.setAdapter(adapter);//绑定适配器

        mViewPager.setOnPageChangeListener(new InternalViewPagerListener());//给viewpager绑定页面改变的监听器
    }


    public static int getScreenWidth(Context ctx) {
        DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }


    @Override
    public void onClick(View v) {//根据点击的tab，来使viewpager滑动到对应的页面
        switch (v.getId()) {
            case R.id.tv_1:
                mViewPager.setCurrentItem(0);
                break;
            case R.id.tv_2:
                mViewPager.setCurrentItem(1);
                break;
            case R.id.tv_3:
                mViewPager.setCurrentItem(2);
                break;

        }

    }


    private class InternalViewPagerListener implements ViewPager.OnPageChangeListener {


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //设置滑条的位置（横坐标）为（位置+偏移量）X宽度（屏幕的宽的三分之一）
            color.setX((position + positionOffset) * getScreenWidth(getApplicationContext()) / 3);
        }


        @Override
        public void onPageSelected(int position) {//这里面的position是指对应的页面的位置0，1，2。。。

            switch (position) {
                case 0:
                    tab1.setTextColor(getResources().getColor(R.color.lightsalmon));

                    tab2.setTextColor(getResources().getColor(R.color.skyblue));
                    tab3.setTextColor(getResources().getColor(R.color.skyblue));
                    break;
                case 1:
                    tab2.setTextColor(getResources().getColor(R.color.lightsalmon));

                    tab1.setTextColor(getResources().getColor(R.color.skyblue));
                    tab3.setTextColor(getResources().getColor(R.color.skyblue));
                    break;
                case 2:
                    tab3.setTextColor(getResources().getColor(R.color.lightsalmon));

                    tab2.setTextColor(getResources().getColor(R.color.skyblue));
                    tab1.setTextColor(getResources().getColor(R.color.skyblue));
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        private List<Fragment> fraglist;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fraglist) {
            super(fm);
            this.fraglist = fraglist;
        }

        public Fragment getItem(int arg0) {
            return fraglist.get(arg0);
        }

        public int getCount() {
            return fraglist.size();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.it_finish:
                finish();
        }
        return true;
    }
}
